package app.auto.designer.intfs.intf;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import app.auto.designer.R;
import app.auto.designer.ActivityBase;
import app.auto.designer.param.ParamActivity;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.intf.JsonUtil;

/**
 * Created by Administrator on 2017/11/3.
 */
public class SetParamActivityDialog extends ActivityBase {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.auto_http_dialog_param);
        String data = getIntent().getStringExtra("data");
        String data_editable = getIntent().getStringExtra("data_editable");

        if(data_editable==null||data_editable.equals("false")){
            View[] views = new View[]{getView(R.id.name), getView(R.id.description), getView(R.id.path), getView(R.id.param)};
            for (View v : views) {
                v.setEnabled(false);
            }
        }
        if (data != null) {

            Map map = (Map) JsonUtil.extractJsonRightValue(data);
            MapViewConf.with(this).pair("name->name").pair("description->description").pair("defaultvalue->defaultvalue").source(map, this).toView();
            String type = (String) map.get("type");
            if (type.toLowerCase().equals("param")) {
                ((RadioButton) findViewById(R.id.param)).setChecked(true);
            }else{
                ((RadioButton) findViewById(R.id.path)).setChecked(true);
            }
        }
        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JSONObject job = new JSONObject();
                String description = ((TextView) findViewById(R.id.description)).getText().toString();
                String name = ((TextView) findViewById(R.id.name)).getText().toString();
                String type = "intfpath";
                if (((RadioButton) findViewById(R.id.param)).isChecked()) {
                    type = "param";
                }
                String defaultvalue = ((TextView) findViewById(R.id.defaultvalue)).getText().toString();

                try {
                    job.put("description", description);
                    job.put("name", name);
                    job.put("defaultvalue", defaultvalue);
                    job.put("type", type);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                setResult(0, new Intent().putExtra("key", name).putExtra("data", job.toString()));
                finish();
            }
        });

    getView(R.id.edit).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(v.getContext(),ParamActivity.class));
        }
    });
    }
}
